package org.bravo369.inflightxt.services;

import org.bravo369.inflightxt.R;
import org.bravo369.inflightxt.utils.Globals;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class GpsLocationListener implements LocationListener {

  private LocationManager locationManager;
  private TextView latView;
  private TextView lonView;
  private TextView altView;

  public GpsLocationListener(Activity currentActivity) {
    latView = (TextView)currentActivity.findViewById(R.id.txtLat);
    lonView = (TextView)currentActivity.findViewById(R.id.txtLon);
    altView = (TextView)currentActivity.findViewById(R.id.txtAlt);
    locationManager = (LocationManager)currentActivity.getSystemService(
        Context.LOCATION_SERVICE);
  }

  public void registerListener() {
    locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER,
        0,
        0,
        this);
  }

  public void unregisterListener() {
    locationManager.removeUpdates(this);
  }

  public void exit() {
    unregisterListener();
  }

  @Override
  public void onLocationChanged(Location location) {
    if (location != null) {
      Globals globals = Globals.getInstance();
      globals.repository.addGpsData(
          location.getLatitude(),
          location.getLongitude(),
          location.getAltitude());
      latView.setText(Double.toString(location.getLatitude()));
      lonView.setText(Double.toString(location.getLongitude()));
      altView.setText(Double.toString(location.getAltitude()));
    }
  }

  @Override
  public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

  }

  @Override
  public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub
  }

  @Override
  public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub
  }
}
